package com.friend.joey.applicationfriend;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Binder;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.support.v7.app.NotificationCompat;

import com.baidu.trace.model.PushMessage;
import com.friend.joey.applicationfriend.R;
import com.friend.joey.applicationfriend.TraceRecord;

/**
 * Created by Joey on 2017/5/29.
 */

public class TraceService extends Service {
    public static final int TRACE_OFF=0;
    public static final int TRACE_ON=1;
    public static final int GATHER_OFF=2;
    public static final int GATHER_ON=3;
    private int state;
    private int state2;
    private boolean gather;
    private boolean start;
    private NotificationManager manager;
    private TraceRecord traceRecord;
    private TraceServiceBinder mBinder=new TraceServiceBinder();
    private TraceServiceListener traceServiceListener;
    public interface TraceServiceListener{
        void onBack(int state,int status,String s);
    }
    public class TraceServiceBinder extends Binder {
        public void setTraceServiceListener(TraceServiceListener listener){
            traceServiceListener=listener;
        }

        public int getProgress(){
            return state;
        }
        public boolean getStart(){
            return start;
        }
        public void startTrace(){
            startTraceService();
        }
        public void startGather(Boolean on){
            startGatherService(on);
        }
        public void stopGather(){
            stopGatherServive();
        }

    }
    public TraceService(){}

    @Override
    public void onCreate() {
        super.onCreate();
        state=TRACE_OFF;
        state2=GATHER_OFF;
        start=false;
        manager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
        getMyForgroundNotification("轨迹服务：服务未开启");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        start=true;
        String name=intent.getStringExtra("entityName");
        gather=intent.getBooleanExtra("gather",false);
        traceRecord = new TraceRecord("myTrace", getApplicationContext());
        traceRecord.setOnTraceListener(new TraceRecord.onTraceListener() {
            @Override
            public void onMBindServiceCallback(int i, String s) {

            }
            @Override
            public void onMStartTraceCallback(int status, String message){
                if (traceServiceListener != null) {
                    traceServiceListener.onBack(state,status,"开启服务："+message);
                }
                if(status==0){
                    state=TRACE_ON;
                    getMyForgroundNotification("轨迹服务开启：轨迹采集未开启");
                    if(gather){
                        startGatherService(true);
                    }
                }else if(state==TRACE_OFF){
                    getMyForgroundNotification("轨迹服务：服务开启失败");
                }
            }
            @Override
            public void onMStopTraceCallback(int status, String message) {
                getMyNotification(2,"轨迹服务已关闭");
            }
            @Override
            public void onMStartGatherCallback(int status, String message) {

                if(status==0){
                    state2=GATHER_ON;
                    getMyForgroundNotification("轨迹服务开启：轨迹采集开启");
                }else if(state2==GATHER_OFF){
                    getMyForgroundNotification("轨迹服务开启：轨迹采集开启失败");
                }
                if (traceServiceListener != null) {
                    traceServiceListener.onBack(state2,status,"开启采集："+message);
                }
            }
            @Override
            public void onMStopGatherCallback(int status, String message) {
                if (status == 0) {
                    state2=GATHER_OFF;
                    getMyForgroundNotification("轨迹服务开启：轨迹采集关闭");
                }
                if (traceServiceListener != null) {
                    traceServiceListener.onBack(state2,status,"关闭采集："+message);
                }
            }
            @Override
            public void onMPushCallback(byte messageNo, PushMessage message) {
                //Toast.makeText(MainActivity.this,"打包回传:"+message.toString(), Toast.LENGTH_SHORT).show();

            }
        });
        startTraceService();
        return super.onStartCommand(intent, flags, startId);
    }
    public void startTraceService(){
        new Thread(new Runnable(){
            @Override
            public void run(){
                traceRecord.startTrace();
            }
        }).start();
    }
    public void startGatherService(Boolean on){
        if(on) {
            //getMyForgroundNotification("轨迹服务开启：正在开启轨迹采集");
            traceRecord.startGather();
        }else{
            getMyForgroundNotification("轨迹服务开启：轨迹采集关闭");
        }

    }
    public void stopGatherServive(){
        getMyForgroundNotification("轨迹服务开启：正在关闭轨迹采集");
        traceRecord.stopGather();
    }
    public void getMyNotification(int i,String s){
        Notification notification=new NotificationCompat.Builder(this)
                .setContentTitle("OldFriend轨迹")
                .setContentText(s)
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.mipmap.ic_launcher)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))
                .setPriority(NotificationCompat.PRIORITY_MAX)
                .build();
        manager.notify(i,notification);
    }
    public void getMyForgroundNotification(String s){
        Notification notification=new NotificationCompat.Builder(this)
                .setContentTitle("OldFriend")
                .setContentText(s)
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.mipmap.ic_launcher)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))
                .build();
        startForeground(1,notification);
    }

    @Override
    public void onDestroy() {
        getMyNotification(2,"轨迹服务关闭");
        stopForeground(true);
        if(state==TRACE_ON) {
            traceRecord.stopTrace();
        }
        super.onDestroy();
    }


    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }
}
